#![windows_subsystem = "windows"]

use cpp_core::{Ptr, StaticUpcast};
use qt_core::{
    q_init_resource, qs, slot, CheckState, ItemDataRole, QBox, QObject, QPtr,
    QSortFilterProxyModel, SlotNoArgs,
};
use qt_gui::{QStandardItem, QStandardItemModel};
use qt_ui_tools::ui_form;
use qt_widgets::{QMessageBox, QLineEdit, QApplication, QListView, QPushButton, QRadioButton, QWidget};
use std::collections::BTreeSet;
use std::rc::Rc;

#[ui_form("../ui/dialog1.ui")]
#[derive(Debug)]
struct Form {
    widget: QBox<QWidget>,
    button1: QPtr<QPushButton>,
    edit1:QPtr<QLineEdit>,
    // remove_selected: QPtr<QPushButton>,
    // list: QPtr<QListView>,
    // show_all: QPtr<QRadioButton>,
    // show_active: QPtr<QRadioButton>,
    // show_completed: QPtr<QRadioButton>,
    // remove_completed: QPtr<QPushButton>,
}

#[derive(Debug)]
struct TodoWidget {
    form: Form,
    source_model: QBox<QStandardItemModel>,
    proxy_model: QBox<QSortFilterProxyModel>,
}

impl StaticUpcast<QObject> for TodoWidget {
    unsafe fn static_upcast(ptr: Ptr<Self>) -> Ptr<QObject> {
        ptr.form.widget.as_ptr().static_upcast()
    }
}

impl TodoWidget {
    fn new() -> Rc<Self> {
        unsafe {
            let this = Rc::new(TodoWidget {
                form: Form::load(),
                source_model: QStandardItemModel::new_0a(),
                proxy_model: QSortFilterProxyModel::new_0a(),
            });
            this.init();
            this
        }
    }

    unsafe fn init(self: &Rc<Self>) {
        for &(text, is_done) in &[
            ("Learn Qt", true),
            ("Learn Rust", true),
            ("Conquer the world", false),
        ] {
            let item = QStandardItem::new().into_ptr();
            item.set_text(&qs(text));
            item.set_checkable(true);
            item.set_check_state(if is_done {
                CheckState::Checked
            } else {
                CheckState::Unchecked
            });
            self.source_model.append_row_q_standard_item(item);
        }

        self.proxy_model.set_source_model(&self.source_model);
        self.proxy_model
            .set_filter_role(ItemDataRole::CheckStateRole.into());
        // self.form.list.set_model(&self.proxy_model);
        self.form.button1.clicked().connect(&self.slot_on_button1_clicked());
        // self.form
        //     .remove_selected
        //     .clicked()
        //     .connect(&self.slot_on_remove_selected_clicked());
        // self.form
        //     .remove_completed
        //     .clicked()
        //     .connect(&self.slot_on_remove_completed_clicked());
        // self.form
        //     .list
        //     .selection_model()
        //     .selection_changed()
        //     .connect(&self.slot_on_list_selection_changed());

        // for button in &[
        //     &self.form.show_completed,
        //     &self.form.show_active,
        //     &self.form.show_all,
        // ] {
        //     button.toggled().connect(&self.slot_on_filter_changed());
        // }

        // self.on_list_selection_changed();
    }

    #[slot(SlotNoArgs)]
    unsafe fn on_button1_clicked(self: &Rc<Self>) {
        self.form.edit1.set_text(&qs("Example"));//&qs("Example")
    }

    fn show(self: &Rc<Self>) {
        unsafe {
            self.form.widget.show();
        }
    }
}

pub fn main() {
    QApplication::init(|_| {
        q_init_resource!("resources");
        let todo_widget = TodoWidget::new();
        todo_widget.show();
        unsafe { QApplication::exec() }
    })
}
